package com.ztn.iot.zhonghaichi2.push;

import com.ztn.base.util.common.HttpClientTool;
import com.ztn.base.util.common.IdTool;
import com.ztn.base.util.common.JsonTool;
import com.ztn.base.util.common.TimeTool;
import com.ztn.iot.zhonghaichi2.properties.ZhcProperties;
import com.ztn.iot.zhonghaichi2.push.req.DataReq;
import com.ztn.iot.zhonghaichi2.push.req.PicturesData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Arrays;

@Component
@Slf4j
public class CameraPushApi {

    @Autowired
    private ZhcProperties zhcProperties;

    public void pushPicture(String siteCode, String base64Str) {
        if (!StringUtils.hasText(siteCode)) {
            log.error("!StringUtils.hasText(siteCode)");
            return;
        }
        int time = TimeTool.time();
        final String date = TimeTool.timeStamp2Date(time);
        DataReq.Item item = new DataReq.Item();
        item.setPicture(Arrays.asList(
                PicturesData.builder().stcd(siteCode).tm(date).picData(base64Str).picName(siteCode+"-"+time+".png").build()
        ));
        DataReq.DataItem dataDataItem = new DataReq.DataItem();
        dataDataItem.setInsert(item);
        DataReq dataReq = new DataReq();
        dataReq.setSysid(IdTool.getUUID());
        dataReq.setDatas(dataDataItem);
        dataReq.setAdcd(zhcProperties.getAdcd());
        dataReq.setToken(zhcProperties.getToken());
        dataReq.setTime(date);
        HttpClientTool.postJsonStrForStr(DataPushApi.URL, JsonTool.toJson(dataReq),null);
    }
}
